﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpExt.Text
{

    public static class TimeSpanParser
    {
        /// <summary>
        /// L'intervallo puo' essere espresso come:
        /// 
        ///     (((dd:)hh:)mm:)ss
        /// 
        /// </summary>
        /// <param name="interval"></param>
        /// <returns></returns>
        public static TimeSpan Parse(String interval)
        {
            int pos, ss = 0, mm = 0, hh = 0, dd = 0;

            pos = interval.LastIndexOf(':');
            if (pos != -1)
            {
                ss = int.Parse(interval.Substring(pos + 1));
                interval = interval.Substring(0, pos);
            }
            pos = interval.LastIndexOf(':');
            if (pos != -1)
            {
                mm = int.Parse(interval.Substring(pos + 1));
                interval = interval.Substring(0, pos);
            }
            pos = interval.LastIndexOf(':');
            if (pos != -1)
            {
                hh = int.Parse(interval.Substring(pos + 1));
                interval = interval.Substring(0, pos);
            }
            if (interval.Length != 0)
            {
                dd = int.Parse(interval);
            }

            return new TimeSpan(dd, hh, mm, ss);
        }
    }
}
